package com.ccys.cms.service.masterData.impl;

import cn.hutool.core.util.ObjectUtil;
import com.ccys.cms.service.masterData.FaqService;
import com.ccys.common.util.StringUtil;
import com.ccys.common.vo.ResponseResult;
import com.ccys.db.dto.masterData.FaqDelDto;
import com.ccys.db.dto.masterData.FaqFindListDto;
import com.ccys.db.dto.masterData.FaqSaveDto;
import com.ccys.db.entity.TblFaq;
import com.ccys.db.mapper.TblFaqMapper;
import com.ccys.db.rely.service.impl.BaseServiceImpl;
import com.ccys.db.service.BaseTblAuditTrailWebService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;

/**
 * @program: WhiteCoat
 * @description: Faq实现类
 * @author: ZhangJingCan
 * @create: 2023-12-12 14:57
 **/

@Service
public class FaqServiceImpl extends BaseServiceImpl<TblFaq> implements FaqService {

    @Resource
    private TblFaqMapper tblFaqMapper;

    /**
     * 用于保存修改状态日志
     */
    @Autowired(required = false)
    private BaseTblAuditTrailWebService baseTblAuditTrailWebService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResponseResult saveFaq(FaqSaveDto dto) {
        //判断 - typeId不为空并且question不为空 - 需要查询是否存在
        if (ObjectUtil.isNotEmpty(dto.getQuestion())) {
            //准备查询条件
            Example example = new Example(TblFaq.class);
            Example.Criteria criteria = example.createCriteria()
                    .andEqualTo("question", dto.getQuestion())
                    .andEqualTo("delFlag", 0);
            //如果typeId存在则需要校验
            if (ObjectUtil.isNotEmpty(dto.getTypeId())) {
                criteria.andEqualTo("typeId", dto.getTypeId());
            }

            //另外需要判断ID是否存在值 - 存在则是修改 - 需要排除自身
            if (ObjectUtil.isNotEmpty(dto.getId())) {
                criteria.andNotEqualTo("id", dto.getId());
            }
            //查询并且判断是否有值 - 有则返回错误信息
            if (ObjectUtil.isNotEmpty(tblFaqMapper.selectOneByExample(example))) {
                return ResponseResult.error("This issue already exists under the current classification!");
            }
        }

        if (ObjectUtil.isNotEmpty(dto.getId()) && ObjectUtil.isNotEmpty(dto.getStatus())) {
            baseTblAuditTrailWebService.saveStatusLog(dto, dto.getStatus());
        }
        if (StringUtil.isEmpty(dto.getId())) {
            dto.setStatus(1);
        }
        //开始新增/修改
        return ResponseResult.update(this.insertOrUpdate(dto));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResponseResult deleteFaq(FaqDelDto dto) {
        //校验ID是否有值
        if (ObjectUtil.isEmpty(dto.getId())) {
            return ResponseResult.error("Parameter Error!");
        }
        //准备数据软删除
        TblFaq faq = new TblFaq();
        faq.setId(dto.getId());
        faq.setDelFlag(1);
        if (tblFaqMapper.updateByPrimaryKeySelective(faq) > 0) {
            return ResponseResult.success("Delete Successful!");
        }
        return ResponseResult.error("Delete Failed!");
    }

    @Override
    public ResponseResult findListFaq(FaqFindListDto dto) {
        return ResponseResult.success(tblFaqMapper.findListFaq(dto));
    }
}
